from django.db import models
from django.core.validators import RegexValidator

# Create your models here.

class ActiveBaseModel(models.Model):
    active = models.SmallIntegerField(default=1,choices=((1,"激活"),(0,"删除")))
    class Meta:
        abstract = True

class UserInfo(models.Model):
    name = models.CharField(max_length=32)
    password = models.CharField(max_length=32)


class Administrator(ActiveBaseModel):
    name = models.CharField(verbose_name="用户名",max_length=32, db_index=True)
    password = models.CharField(verbose_name="密码",max_length=32)
    mobile = models.CharField(verbose_name="电话",max_length=11, unique=True,db_index=True)
    create_time = models.DateTimeField(verbose_name="创建时间",auto_now_add=True)

class Leval(ActiveBaseModel):
    title = models.CharField(verbose_name="级别", max_length=32)
    present = models.IntegerField(verbose_name="折扣")
    def __str__(self):
        return self.title

class Customer(ActiveBaseModel):
    name = models.CharField(verbose_name="用户名",max_length=32, db_index=True)
    password = models.CharField(verbose_name="密码",max_length=32)
    mobile = models.CharField(verbose_name="电话", max_length=11, unique=False, db_index=True,validators=[RegexValidator(r'\d{11}',"手机号码格式有误")])
    balance = models.DecimalField(verbose_name="账户余额",default=0,max_digits=10,decimal_places=2)
    leval = models.ForeignKey(verbose_name="级别", to="Leval",on_delete=models.CASCADE)
    create_time = models.DateTimeField(verbose_name="创建时间",auto_now_add=True)

class PricePolicy(models.Model):
    count = models.IntegerField(verbose_name="数量")
    price = models.DecimalField(verbose_name="价格",default=0,max_digits=10,decimal_places=2)

class Order(ActiveBaseModel):
    status_choices = (
        (1,"待执行"),
        (2,"正在执行"),
        (3,"已完成"),
        (4,"失败"),
        (5,"已撤单")
    )
    status = models.SmallIntegerField(verbose_name="状态",choices=status_choices, default=1)
    oid = models.CharField(verbose_name="订单号",max_length=64,unique=True,db_index=True)
    url = models.URLField(verbose_name="视频地址",db_index=True)
    count = models.IntegerField(verbose_name="数量")
    price = models.DecimalField(verbose_name="原价",default=0,max_digits=10,decimal_places=2)
    real_price = models.DecimalField(verbose_name="实际价格",default=0,max_digits=10,decimal_places=2)
    old_view_count = models.CharField(verbose_name="原播放量",max_length=32,default="0")
    customer = models.ForeignKey(verbose_name="客户",to="Customer",on_delete=models.CASCADE)
    create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    memo = models.TextField(verbose_name="备注",null=True,blank=True)

class TransactionRecord(ActiveBaseModel):
    """交易记录"""
    charge_type_class_mapping = {
        1:"success",
        2:"danger",
        3:"default",
        4:"info",
        5:"primary"
    }
    charge_type_choice = ((1,"充值"),(2,"扣款"),(3,"创建订单"),(4,"删除订单"),(5,"撤单"))
    charge_type = models.SmallIntegerField(verbose_name="类型",choices=charge_type_choice)
    customer = models.ForeignKey(verbose_name="客户",to="Customer",on_delete=models.CASCADE)
    amount = models.DecimalField(verbose_name="金额",default=0,max_digits=10,decimal_places=2)
    creator = models.ForeignKey(verbose_name="管理员",to="Administrator",on_delete=models.CASCADE,null=True,blank=True)
    order_oid = models.CharField(verbose_name="订单号", max_length=64, null=True, blank=True, db_index=True)
    create_datetime = models.DateTimeField(verbose_name="交易时间", auto_now_add=True)
    memo = models.TextField(verbose_name="备注", null=True, blank=True)